<?php
namespace Controller;

class RegisterController {
	/**
	 * @return String $xhtml
	 */
	public function DoControll($database) {
		$xhtml = "";
		$messageid = 0;
		$message = "";

		$rv = new \View\RegisterView();
		$rh = new \Model\RegisterHandler($database);
		$mail = new \Model\RegMail();
		//Kollar om användaren försökt registrera sig och returnerar ett meddelande.

		if ($rv -> TriedToRegister()) {
			$messageid = $rh -> RegisterUser($rv -> GetUsername(), 
											$rv -> GetFirstname(), 
											$rv -> GetLastname(), 
											$rv -> GetSocnr(),
											$rv -> GetAddress(),
											$rv -> GetAddress2(),
											$rv -> GetZip(),
											$rv -> GetTown(),
											$rv -> GetPhone(),
											$rv -> GetEmail(),
											$rv -> GetPassword(), 
											$rv -> GetPassword2());

			if ($messageid == \Model\RegisterHandler::PASSWORD_MISSMATCH) {
				$message .= $rv -> TryRegMess(\View\RegisterView::PASSWORD_ON_BOTH_WRONG);
			}
			if ($messageid == \Model\RegisterHandler::FIELDS_EMPTY) {
				$message .= $rv -> TryRegMess(\View\RegisterView::FIELDS_EMPTY);
			}
			if ($messageid == \Model\RegisterHandler::USERNAME_IN_USE) {
				$message .= $rv -> TryRegMess(\View\RegisterView::USERNAME_IN_USE);
			}
			if ($messageid == \Model\RegisterHandler::PASSWORD_WRONG_FORMAT) {
				$message .= $rv -> TryRegMess(\View\RegisterView::PASSWORD_WRONG_FORMAT);
			}
			if ($messageid == \Model\RegisterHandler::REG_SUCCESS) {
				$message .= $rv -> TryRegMess(\View\RegisterView::REG_SUCCESS);

			}
		}
		if ($messageid == \Model\RegisterHandler::REG_SUCCESS)
		{
			$xhtml .= $message;
			$mail -> mailTO($rv -> GetEmail(), $rv -> GetUserName());
			header('Location: http://eddmedia.se');
		}
		else{
			$xhtml .= $message;
			$xhtml .= $rv -> DoRegisterForm();
			
		}
		return $xhtml;
	}

}
